import request from "@/utils/request";
import type { Question, QuestionStatus } from "@/types/question";

// API响应类型
interface QuestionListResponse {
  total: number;
  questions: Question[];
}

// 查询参数类型
interface QuestionQueryParams {
  skip?: number;
  limit?: number;
  keyword?: string;
  status?: QuestionStatus;
  category?: string;
}

// 创建问题请求类型
interface CreateQuestionRequest {
  title: string;
}

// 更新问题请求类型
interface UpdateQuestionRequest {
  title?: string;
  status?: QuestionStatus;
}

// 获取问题列表
export const getQuestions = (
  params?: QuestionQueryParams
): Promise<QuestionListResponse> => {
  return request.get("/questions/", { params });
};

// 获取问题详情
export const getQuestionById = (questionId: number): Promise<Question> => {
  return request.get(`/questions/${questionId}`);
};

// 创建新问题
export const createQuestion = (
  data: CreateQuestionRequest
): Promise<Question> => {
  return request.post("/questions/", data);
};

// 更新问题信息
export const updateQuestion = (
  questionId: number,
  data: UpdateQuestionRequest
): Promise<Question> => {
  return request.put(`/questions/${questionId}`, data);
};

// 删除问题
export const deleteQuestion = (
  questionId: number
): Promise<{
  message: string;
  id: number;
  title: string;
  category: string;
}> => {
  return request.delete(`/questions/${questionId}`);
};

// 切换问题状态
export const toggleQuestionStatus = (questionId: number): Promise<Question> => {
  return request.patch(`/questions/${questionId}/toggle-status`);
};
